﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace APPFrameworkStudy.Domain.Mapping
{
    public class Utility
    {
        /// <summary>
        /// 加载实体映射信息
        /// </summary>
        /// <param name="modelBuilder"></param>
        public static void LoadMapping(DbModelBuilder modelBuilder)
        {
            //如果mapping在单独的项目中,要加载对应的assembly
            //Assembly assembly = Assembly.LoadFrom("APPFrameworkStudy.Models.dll");
            Assembly assembly = Assembly.GetExecutingAssembly();
            var typesToRegister = assembly.GetTypes()
           .Where(type => !String.IsNullOrEmpty(type.Namespace))
           .Where(type => type.BaseType != null
                 && type.BaseType.Name.IndexOf("BaseDomainMapping") != -1);
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
        }
    }
}
